//
//  YZImageManager.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/27.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZImageManager.h"
#import "YZMediaManager.h"
#import "YZFileTool.h"

@implementation YZImageManager


+ (instancetype)shareManager {
    static YZImageManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    
    return manager;
}



- (void)requestWithUrlStr:(NSString *)urlStr
                mediaType:(NSString *)mediaType
                    block:(GetOriginImagPathBlock)orginImageUrlBlock {
    NSURLSessionConfiguration *sessionConf = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:sessionConf];
    
    NSURLSessionDownloadTask *task = [urlSession downloadTaskWithURL:[NSURL URLWithString:urlStr] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        NSString *mainPath = @"";
        NSString *suffix = @"";
        if ([mediaType isEqualToString:@"video/mp4"]) {
            suffix = @".mp4";
//            mainPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//
//            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:kChatVideoPath];
//
//            NSFileManager *fileManager = [NSFileManager defaultManager];
//            BOOL isDirExist = [fileManager fileExistsAtPath:path];
//            if (!isDirExist) {
//                BOOL isCreatDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//                if (!isCreatDir) {
//                    NSLog(@"create folder failed");
//                }
//            }
            
            mainPath = [YZFileTool createPathWithChildPath:kChatVideoPath];
            
//            mainPath = [mainPath stringByAppendingPathComponent:kChatVideoPath];
        } else if ([mediaType isEqualToString:@"image/*"]) {
            suffix = @".png";
            mainPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            mainPath = [mainPath stringByAppendingPathComponent:kMyPic];
        }
        NSString *fileName = [NSString stringWithFormat:@"%@%@",response.suggestedFilename,suffix];
        NSString *fullPath = [mainPath stringByAppendingPathComponent:fileName];
        BOOL saveVideoSuccess = [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
        if (saveVideoSuccess) {
            NSLog(@"fullPath----->%@",fullPath);
            self.originImagePath = fullPath;
            if (orginImageUrlBlock) {
                orginImageUrlBlock(fullPath);
            }
        } else {
            NSLog(@"视频保存失败");
        }
        
    }];
    
    [task resume];
}

@end
